package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

import static com.cskaoyan.shopping.constants.GlobalConstants.RECOMMEND_PANEL_ID;

/**
 * @program: duolaimall
 * @description:
 * @author: Lexis
 * @create: 2022-05-19 20:41
 **/
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    ProductConverter productConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse productResponse = new ProductDetailResponse();
        try {
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto productDetailDto = productConverter.itemToDetailDto(item);
            productDetailDto.setDetail(itemDesc.getItemDesc());
            productResponse.setProductDetailDto(productDetailDto);
            productResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(productResponse, e);
        }
        return productResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse allProductResponse = new AllProductResponse();

        try {
            request.requestCheck();

            // 页码
            Integer page = request.getPage();
            // 数据量
            Integer limit = request.getSize();
            // 1升序 -1降序
            String sort = request.getSort();
            // 商品类别
            Long cid = request.getCid();
            // 最低价
            Integer priceGt = request.getPriceGt();
            // 最高价
            Integer priceLte = request.getPriceLte();
            //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
            PageHelper.startPage(page, limit);

            Example example = new Example(Item.class);
            Example.Criteria criteria = example.createCriteria();
            if (sort != null) {
                if (sort.equals("-1")) {
                    example.setOrderByClause("price DESC");
                } else if (sort.equals("1")) {
                    example.setOrderByClause("price");
                }
            }
            if (priceGt != null) {
                criteria.andGreaterThan("price", priceGt);
            }
            if (priceLte != null) {
                criteria.andLessThanOrEqualTo("price", priceLte);
            }
            if (cid != null) {
                criteria.andEqualTo("cid", cid);
            }
            List<Item> items = itemMapper.selectByExample(example);
            List<ProductDto> productDtos = productConverter.items2Dto(items);

            PageInfo<Item> pageInfo = new PageInfo<>(items);
            long total = pageInfo.getTotal();
            allProductResponse.setTotal(total);
            allProductResponse.setProductDtoList(productDtos);
            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse, e);
        }
        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();
        Panel p = new Panel();
        p.setId(RECOMMEND_PANEL_ID);
        try {
            List<Panel> panels = panelMapper.select(p);
            List<PanelDto> panelDtos = contentConverter.panelToDto(panels);
            for (PanelDto panel : panelDtos) {
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
                // 限制条目，多了就去掉最后一个
                // if (panelContentItemDtos.size() > panel.getLimitNum()) {
                //     panelContentItemDtos.remove(panelContentItemDtos.size() - 1);
                // }
                panel.setPanelContentItems(panelContentItemDtos);
            }
            recommendResponse.setPanelContentItemDtos(panelDtos);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse, e);
        }
        return recommendResponse;
    }

    @Override
    public AllItemResponse getAllItems() {
        AllItemResponse response = new AllItemResponse();
        try {

            List<Item> items = itemMapper.selectAll();
            List<ItemDto> itemDtos = productConverter.items2ItemDtos(items);
            response.setAllItems(itemDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
